package com.myrent.controller;

import com.myrent.common.ReturnData;
import com.myrent.entity.User;
import com.myrent.service.UserService;
import com.myrent.utils.JwtTokenUtil;
import com.myrent.utils.RedisUtil;
import com.myrent.utils.ValidatedUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Validated
@RestController
public class UserController {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;

    @PostMapping("/user/login")
    public ReturnData login(User user,
                            @RequestParam("isRememberMe") Boolean isRememberMe,
                            HttpServletRequest request) {
        if (!ValidatedUtil.validate(user)) {
            return ReturnData.fail(-2, "手机号或者密码格式输入不对");
        }
        Boolean isLoginSuccess = userService.login(user.getPhoneNum(), user.getPassword());
        if (isLoginSuccess) {
            user = userService.getUserByPhoneNum(user.getPhoneNum());
            request.setAttribute("userId", user.getUserId());
            if (isRememberMe) {
                try {
                    //生成一个随机的不重复的uuid
                    UUID uuid = UUID.randomUUID();
                    request.setAttribute("uuid", uuid.toString());
                    String token = JwtTokenUtil.createToken(user.getUserId(), null ,uuid.toString());
                    user.setPassword(null);
                    user.setSalt(null);
                    redisUtil.set("user:" + user.getUserId(), uuid.toString(), 60 * 60 * 24 * 7L);
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("token", token);
                    map.put("user", user);
                    return ReturnData.success(map);
                } catch (Exception e) {
                    log.info("登录失败{}", e.getMessage());
                }
            }
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("user", user);
            return ReturnData.success(map);
        } else {
            return ReturnData.fail(-1, "登入失败,请检查用户名或密码");
        }
    }

    /**
     *普通用户的注册
     */
    @RequestMapping("/user/register")
    public ReturnData userRegister(User user) {
        if (!ValidatedUtil.validate(user)) {
            return ReturnData.fail(-1, "请检查参数条件");
        }
        //没有手机验证码的操作，检验重复
        if (userService.getUserByPhoneNum(user.getPhoneNum()) == null) {
            userService.register(user);
            return ReturnData.success();
        }
        return ReturnData.fail(-1, "用户已存在！");
    }




}